import React from 'react';
import { Modal, Button, Input, Form } from 'antd';

interface PasswordModalProps {
  visible: boolean;
  username: string;
  userId: string;
  onCancel: () => void;
  onSubmit: (values: any) => Promise<void>;
}

const PasswordModal: React.FC<PasswordModalProps> = ({
  visible,
  username,
  userId,
  onCancel,
  onSubmit,
}) => {
  const [form] = Form.useForm();

  const handleSubmit = async (values: any) => {
    try {
      await onSubmit(values);
      form.resetFields();
    } catch (error) {
      console.error('修改密码失败:', error);
    }
  };

  const handleCancel = () => {
    form.resetFields();
    onCancel();
  };

  return (
    <Modal
      title="修改密码"
      open={visible}
      onCancel={handleCancel}
      footer={null}
    >
      <Form form={form} onFinish={handleSubmit} layout="vertical" name="modifyPassword">
        <Form.Item name="userId" initialValue={userId} hidden>
          <Input type="hidden" />
        </Form.Item>
        <Form.Item
          label="用户名"
          name="username"
          initialValue={username}
          rules={[{ required: true, message: '请输入用户名' }]}
        >
          <Input placeholder="请输入用户名" disabled={true} />
        </Form.Item>
        <Form.Item
          label="原密码"
          name="originPassword"
          rules={[{ required: true, message: '请输入原密码' }]}
        >
          <Input.Password placeholder="请输入原密码" />
        </Form.Item>
        <Form.Item
          label="新密码"
          name="newPassword"
          rules={[
            { required: true, message: '请输入新密码' },
            ({ getFieldValue }) => ({
              validator(_, value) {
                if (!value || getFieldValue('originPassword') != value) {
                  return Promise.resolve();
                }
                return Promise.reject(new Error('新密码不能与原密码一致'));
              },
            }),
          ]}
        >
          <Input.Password placeholder="请输入新密码" />
        </Form.Item>
        <Form.Item
          label="确认密码"
          name="confirmPassword"
          rules={[
            { required: true, message: '请确认新密码' },
            ({ getFieldValue }) => ({
              validator(_, value) {
                if (!value || getFieldValue('newPassword') === value) {
                  return Promise.resolve();
                }
                return Promise.reject(new Error('两次输入的密码不一致'));
              },
            }),
          ]}
        >
          <Input.Password placeholder="请确认新密码" />
        </Form.Item>
        <Form.Item>
          <Button type="primary" htmlType="submit">
            提交
          </Button>
        </Form.Item>
      </Form>
    </Modal>
  );
};

export default PasswordModal; 